package morenoapp.tools;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.imageio.ImageIO;

/**
 *
 * @author root
 */
public class ScaleIconsFromOrigin {
    public static void main(String[] args) throws IOException {
        Map<String, Integer> nameSizeMap = new LinkedHashMap();
        File path = new File("src/morenoapp/resources/new");
        for(File f:path.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".png");
            }
        })){
            String name = f.getName();
            int size = Integer.parseInt(name.substring(name.lastIndexOf("-")+1,name.lastIndexOf(".")));
            String key = name.substring(0, name.lastIndexOf("-"));

            Integer existsSize = nameSizeMap.get(key);
            if (existsSize==null || existsSize<size){
                nameSizeMap.put(key,size);
            }
        }
        System.out.println(nameSizeMap);

        for(Entry<String,Integer> e:nameSizeMap.entrySet()){
            for(int newSize:new int[]{32,16}){
                if (newSize<e.getValue()){
                    String oldName = e.getKey()+"-"+e.getValue()+".png";
                    String newName = e.getKey()+"-"+newSize+".png";
                    BufferedImage src = ImageIO.read(new File(path, oldName));
                    BufferedImage dst = new BufferedImage(newSize, newSize, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g = (Graphics2D) dst.getGraphics();
                    double scale = (double)newSize/(double)e.getValue();
                    AffineTransform at = new AffineTransform();
                    at.scale(scale, scale);
                    g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
                    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                    g.drawImage(src, at, null);
                    g.dispose();
                    ImageIO.write(dst, "png", new File(path, newName));
                }
            }
        }
    }
}
